package com.openbravo.pos.base;

import java.io.File;
import java.util.List;
import java.util.regex.Matcher;

import com.openbravo.data.basic.BasicException;
import com.openbravo.data.loader.BatchSentence;
import com.openbravo.data.loader.BatchSentenceResource;
import com.openbravo.data.loader.I_Session;

import net.xeoh.plugins.base.PluginManager;
import net.xeoh.plugins.base.impl.PluginManagerFactory;
import cn.ekuma.data.log.DataLogicLoger;
import cn.ekuma.epos.datalogic.I_DataLogicSystem;
import cn.ekuma.rabbit.ModelManager;
import cn.ekuma.rabbit.imp.AbstractApp;

public abstract class AbstractPosApp extends AbstractApp {

	public final static PluginManager pmf = PluginManagerFactory.createPluginManager();
	protected I_Session session;
	
	protected I_DataLogicSystem m_dlSystem;
	
	public AbstractPosApp(AppProperties props) {
		super(new ModelManager(pmf,props));
		pmf.addPluginsFrom(new File("plugins/").toURI());
	}

   public abstract boolean initLocalModel();
	
	public void stop() {
		if(session!=null)
		   session.close();
		super.stop();
	}
	
	
	public void initLoger(String name, String host) {
		DataLogicLoger.init(session, name, host);
	}
	

	protected String readDataBaseVersion() {
        try {
            return m_dlSystem.findVersion();
        } catch (BasicException ed) {
            return null;
        }
    }

	public I_DataLogicSystem getM_dlSystem() {
		return m_dlSystem;
	}
	
	
	
	protected List exeSQLScript(String sScript) throws BasicException{
		BatchSentence bsentence = new BatchSentenceResource(session, sScript);
        bsentence.putParameter("APP_ID", Matcher.quoteReplacement(AppLocal.APP_ID));
        bsentence.putParameter("APP_NAME", Matcher.quoteReplacement(AppLocal.APP_NAME));
        bsentence.putParameter("APP_VERSION", Matcher.quoteReplacement(AppLocal.APP_VERSION));
        //bsentence.putParameter("AD_CLIENT_ID", Matcher.quoteReplacement(AppConfig.getAppProperty(Adempiere.AD_CLIENT_ID)));
        //bsentence.putParameter("AD_ORG_ID", Matcher.quoteReplacement(AppConfig.getAppProperty(Adempiere.AD_ORG_ID)));

        return bsentence.list();
	}
}


